#include "SeveralStacks.h"
/* Undocumented :(
typedef unsigned char bool;

typedef struct SeveralStacks {
	int data[NMAX];
	int next[NMAX];
	int top[KMAX];
	int freeTop;
} SeveralStacks;
*/

void
Init(SeveralStacks *ss)
{
	register int i;
	for (i = 0; i < KMAX; ++i)
		ss->top[i] = -1;
	for (i = 0; i < NMAX - 1; ++i)
		ss->next[i] = i+1;

	ss->next[NMAX-1] = -1;
	ss->freeTop = 0;
}

void
Push(SeveralStacks *ss, int snum, int key)
{
	int i = ss->freeTop;
	ss->freeTop = ss->next[i];
	ss->data[i] = key;
	ss->next[i] = ss->top[snum];
	ss->top[snum] = i;
}

int
Pop(SeveralStacks *ss, int snum)
{
	int i = ss->top[snum];
	ss->top[snum] = ss->next[i];
	ss->next[i] = ss->freeTop;
	ss->freeTop = i;
	return ss->data[i];
}

int
Top(const SeveralStacks *ss, int snum)
{
	return ss->data[ss->top[snum]];
}

bool
IsEmpty(const SeveralStacks *ss, int snum)
{
	return ss->top[snum] == -1;
}
